﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.Objective;
using Jumpton.Persistent;
using Jumpton.SFA.Server.BusinessRule;

/// <summary>
/// 目标类型维护
/// Update By:Allen Jiang
/// Update Time:2008-12-22
/// Description:
///     修改目标类型维护在一个页面。
/// </summary>
public partial class Objective_ObjectiveDetailListView : PageBase
{
    public string ObjectiveID = "";
    public static string keyid = "";
    
    ObjectiveManager manager = new ObjectiveManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["OID"] != null)
            ObjectiveID = Request.QueryString["OID"].ToString();
       
        if (!IsPostBack)
        {
            txtStartDate.Value = DateTime.Now.AddMonths(-1).ToString("yyyy-MM");
            txtEndDate.Value = DateTime.Now.ToString("yyyy-MM");
            ddlBind();
           
            ObjectiveBind();


            
        }
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    ///

    public void ddlBind()
    {
        ObjectTable ot= manager.SelectObjectiveList(MyStateManager.DomainID, "");
        drpObjective.DataSource = ot;
        drpObjective.DataTextField = "NAME";
        drpObjective.DataValueField = "ID";
        drpObjective.DataBind();

        drpObjective.Items.Insert(0, new ListItem("--全部--",""));


        ddl_kind.DataSource = new DictionaryItemsManager().GetItemList(DictionaryEnum.ObjectiveType, MyStateManager.DomainID);
        ddl_kind.DataTextField = "NAME";
        ddl_kind.DataValueField = "CODE";
        ddl_kind.DataBind();
        ddl_kind.Items.Insert(0, new ListItem("--请选择--", ""));

        if (ObjectiveID != "")
        {
            drpObjective.SelectedValue = ObjectiveID;
            drpObjective.Enabled = false;
        }
    }

    public void ObjectiveBind()
    {
        string where = "";

        if (txtStartDate.Value != "" && txtEndDate.Value != "")
        {
            where += " AND od.YEAR_MONTH BETWEEN "+getIntDate(txtStartDate.Value) +" AND "+getIntDate(txtEndDate.Value);
        }
        if (drpObjective.SelectedValue != "")
        {
            where += " AND od.OBJECTIVE_ID="+drpObjective.SelectedValue;
        }

        if (ddl_kind.SelectedValue != "")
        {
            where += " AND o.OBJECTIVETYPE=" + ddl_kind.SelectedValue;
        }

        if (!new PersonManager().IsManager(MyStateManager.PersonID, MyStateManager.DomainID))
        {
            where += " AND od.OBJECTID="+MyStateManager.UserID;
        }

        ObjectTable ot = null;
        ot = manager.SelectObjectiveDetailViewList(MyStateManager.DomainID, where);
        GridPager1.DataSource = ot;
        GridPager1.GridView = GridView1;
        GridPager1.BindData();
        GridPager1.Visible = GridView1.Rows.Count > 0;

       
    }

   
  

   

   
   

  
    public string getObjectType(string value)
    {
        string type = "";
        try
        {
                type = new DictionaryItemsManager().Load(DictionaryEnum.ObjectiveType, MyStateManager.DomainID,value).Name;
        }
        catch { }
        return type;
    }

    public string getName(string type, string id)
    {
        string name = "";
        try
        {
            switch (type)
            {
                case "1":
                    USERS u = new Jumpton.SFA.Server.BusinessRule.System.UserManager().GetUserByID(decimal.Parse(id));
                    if (u != null && u.PERSON != null)
                        name = u.PERSON.Name;
                    break;
                case "2":
                    type = "客户目标";
                    break;
                case "3":
                    type = "品牌目标";
                    break;
                default:
                    type = "未知";
                    break;
            }
        }
        catch { }
        return name;
    }

   

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("ObjectiveDetailMaintain.aspx?OID=" + ObjectiveID+"&KEYID="+keyid);

    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        ObjectiveBind();
    }

    public string getIntDate(string value)
    {
        return value.Replace("-", "").Replace("/", "");
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lb = e.Row.FindControl("LinkButton2") as LinkButton;
            if (lb != null)
            {
                if (int.Parse(lb.CommandArgument)<int.Parse(DateTime.Now.ToString("yyyyMM")))
                {
                    lb.Enabled = false;
                }
            }
        }
        
    }
}
